package com.bytedance.article.common.a.b;

import android.content.Context;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;

public final class a {
    public static final ConcurrentHashMap b;

    static {
        b = new ConcurrentHashMap();
    }

    public Context a;
    private volatile JSONObject c;

    public a(Context arg2) {

        if (arg2 != null) {
            this.a = arg2.getApplicationContext();
        }
    }

    static void a(a arg2, String arg3) throws IOException, JSONException {
        if (!TextUtils.isEmpty(((CharSequence) arg3))) {
            File v0 = new File(arg3);
            if (v0.exists()) {
                arg2.a(v0);
                if ((v0.exists()) && !v0.delete()) {
                    throw new RuntimeException("file delete failed");
                }
            }
        }
    }

    private void a(File arg7) throws IOException, JSONException {
        FileInputStream v1 = null;
        LinkedList v4;
        if (arg7.exists()) {
            FileInputStream v2 = null;
            try {
                v4 = new LinkedList();
                v1 = new FileInputStream(arg7);
                BufferedReader v2_1 = new BufferedReader(new InputStreamReader(((InputStream) v1)));
                int v0_1;
                for (v0_1 = 0; true; ) {
//                    label_14:
                    String v5 = v2_1.readLine();
                    if (v5 == null) {
                        break;
                    }

                    v4.add(v5);
                    ++v0_1;
                    if (v0_1 <= 100) {
                        continue;
                    }
                    this.a(v4);
                    v0_1 = 0;
                }

                this.a(v4);
            } catch (Throwable v0) {
                v0.printStackTrace();
            } finally {
                if (v1 != null) {
                    ((InputStream) v1).close();
                }
            }
        }
    }

    private void a(LinkedList arg5) {
        JSONArray v2 = null;
        JSONObject v1 = null;
        try {
            v1 = new JSONObject();
            v2 = new JSONArray();
            while (!arg5.isEmpty()) {
                Object v0_1 = arg5.poll();
                try {
                    v2.put(new JSONObject(((String) v0_1)));
                } catch (Exception v0_2) {
                }
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }

        try {
            if (v2.length() > 0) {
                v1.put("data", v2);
            }

            if (v1.length() <= 0) {
                return;
            }

            if (this.c == null) {
                this.c = com.bytedance.framwork.core.monitor.c.j().l();
            }

            v1.put("header", this.c);
            android.support.design.a.l(v1.toString());
        } catch (Throwable v0) {
            label_31:
            v0.printStackTrace();
        }
    }

    public static final class a$a {
        public long a;
        public boolean b;

        public a$a() {
            this.a = 0;
            this.b = false;
        }
    }
}

